if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
  puts 'React Native new arch enabled (RCT_NEW_ARCH_ENABLED = 1)'
end

def node_require(script)
  # Resolve script with node to allow for hoisting
  require Pod::Executable.execute_command('node', ['-p',
    "require.resolve(
      '#{script}',
      {paths: [process.argv[1]]},
    )", __dir__]).strip
end

node_require('react-native/scripts/react_native_pods.rb')
node_require('react-native-permissions/scripts/setup.rb')

platform :ios, min_ios_version_supported

install! 'cocoapods', :deterministic_uuids => false

# Comment unwanted permissions
setup_permissions([
  'AppTrackingTransparency',
  'Bluetooth',
  'Calendars',
  'Camera',
  'Contacts',
  'FaceID',
  'LocationAccuracy',
  'LocationAlways',
  'LocationWhenInUse',
  'MediaLibrary',
  'Microphone',
  'Motion',
  'Notifications',
  'PhotoLibrary',
  'PhotoLibraryAddOnly',
  'Reminders',
  'Siri',
  'SpeechRecognition',
  'StoreKit',
])

def shared_pods
    config = use_native_modules!

    use_react_native!(
      :path => config[:reactNativePath],
      :app_path => "#{Pod::Config.instance.installation_root}/.."
    )

    # Shared pods
    pod 'react-native-slider', :path => '../node_modules/@react-native-community/slider'
end

target 'example' do
  shared_pods
end

target 'example-ci' do
  shared_pods
end

post_install do |installer|
  config = use_native_modules!
  react_native_post_install(
    installer,
    config[:reactNativePath],
    :mac_catalyst_enabled => false
  )

  extract_react_native_version_constants
end


def extract_react_native_version_constants
  require 'json'
  
  Pod::UI.notice "POST INSTALL"
  puts "Extracting React Native version from package.json..."
  
  package_json_path = "../package.json"
  
  begin
    package_content = File.read(package_json_path)
    package_data = JSON.parse(package_content)
    
    rn_version = nil
    if package_data['dependencies'] && package_data['dependencies']['react-native']
      rn_version = package_data['dependencies']['react-native']
    elsif package_data['devDependencies'] && package_data['devDependencies']['react-native']
      rn_version = package_data['devDependencies']['react-native']
    end
    
    unless rn_version
      Pod::UI.warn "Warning: react-native not found in package.json dependencies"
      return
    end
    
    Pod::UI.notice "React Native version from package.json: #{rn_version}"
    
    generate_rn_version_header(rn_version, package_json_path)
end
end


def generate_rn_version_header(rn_version, source_path = nil)
  puts " Generating React Native version header..."
  
  clean_version = rn_version.gsub(/^[\^~>=<]+/, '')
  version_parts = clean_version.split('.')
  
  major = version_parts[0]&.to_i || 0
  minor = version_parts[1]&.to_i || 0  
  patch = version_parts[2]&.to_i || 0
  
  output_header = "ReactNativeVersionExtracted.h"
  
  header_content = <<~HEADER
    //
    // ReactNativeVersionExtracted.h
    // React Native version constants extracted from package.json
    // Generated on: #{Time.now}
    // Source: #{File.expand_path(source_path)}
    // Original version string: #{rn_version}
    //
    
    #ifndef ReactNativeVersionExtracted_h
    #define ReactNativeVersionExtracted_h
    
    #define REACT_NATIVE_VERSION_MAJOR #{major}
    #define REACT_NATIVE_VERSION_MINOR #{minor}
    #define REACT_NATIVE_VERSION_PATCH #{patch}
    
    #endif /* ReactNativeVersionExtracted_h */
  HEADER
  
  begin
    File.write(output_header, header_content)
    
    Pod::UI.notice "Successfully generated #{output_header}"
    puts " Version constants:"
    puts "   #define REACT_NATIVE_VERSION_MAJOR #{major}"
    puts "   #define REACT_NATIVE_VERSION_MINOR #{minor}"
    puts "   #define REACT_NATIVE_VERSION_PATCH #{patch}"
    
    return { major: major, minor: minor, patch: patch }
    
  rescue => e
    Pod::UI.error "❌ Error writing version header: #{e.message}"
    return nil
  end
end
